package cn.iocoder.yudao.module.book.dal.mysql.record;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.book.dal.dataobject.record.RecordDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

/**
 * 阅读记录 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface RecordMapper extends BaseMapperX<RecordDO> {

    @Select("SELECT * FROM book_record ORDER BY create_time ASC LIMIT 1")
    RecordDO selectEarliestRecord();

    // 查询今日阅读数量
    @Select("SELECT COUNT(*) \n" +
        "    FROM book_record \n" +
        "    WHERE DATE(create_time) = CURDATE()")
    int countTodayRecords();

    // 查询本周阅读数量（周一 00:00:00 到周日 23:59:59）
    @Select("SELECT COUNT(*) \n" +
        "    FROM book_record \n" +
        "    WHERE create_time >= DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY)\n" +
        "      AND create_time < DATE_ADD(DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()) DAY), INTERVAL 7 DAY)")
    int countThisWeekRecords();

    // 查询本月阅读数量（当月 1 号 00:00:00 到月末 23:59:59）
    @Select("SELECT COUNT(*) \n" +
        "    FROM book_record \n" +
        "    WHERE create_time >= DATE_FORMAT(CURDATE(), '%Y-%m-01') \n" +
        "      AND create_time < DATE_ADD(DATE_FORMAT(CURDATE(), '%Y-%m-01'), INTERVAL 1 MONTH)")
    int countThisMonthRecords();

    //  查询总阅读量
    @Select("SELECT COUNT(*) FROM book_record ")
    int countAllRecords();

}